﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;


namespace Publish.Client.UserControls
{
    public class CustomTab : System.ComponentModel.Component
    {
        #region 属性

        /// <summary>
        /// 主窗口
        /// </summary>
        [Category("自定义-窗口"), Description("主窗口")]
        public Form MainForm { get; set; }

        /// <summary>
        /// 主窗口
        /// </summary>
        [Category("自定义-窗口"), Description("父容器")]
        public ScrollableControl ParentPenel { get; set; }

        /// <summary>
        /// TabControl对象
        /// </summary>
        [Category("自定义-窗口"), DefaultValue(typeof(TabControl)), Description("TabControl")]
        public TabControl TabControl { get; set; }

        /// <summary>
        /// 标签默认背景
        /// </summary>
        [Category("自定义-样式"), DefaultValue(typeof(Image)), Description("标签默认背景")]
        public Image Tab_bg_default { get; set; }

        /// <summary>
        /// 标签选中背景
        /// </summary>
        [Category("自定义-样式"), DefaultValue(typeof(Image)), Description("标签选中背景")]
        public Image Tab_bg_selected { get; set; }

        /// <summary>
        /// 标签鼠标经过背景
        /// </summary>
        [Category("自定义-样式"), DefaultValue(typeof(Image)), Description("标签鼠标经过背景")]
        public Image Tab_bg_after { get; set; }

        /// <summary>
        /// 导航背景色
        /// </summary>
        [Category("自定义-样式"), DefaultValue(typeof(Color)), Description("导航背景色")]
        public Color Tab_BackColor { get; set; }

        /// <summary>
        /// 字体颜色
        /// </summary>
        [Category("自定义-样式"), Description("字体颜色")]
        public Color Tab_ForeColor { get; set; }

        /// <summary>
        /// 选中字体颜色
        /// </summary>
        [Category("自定义-样式"), DefaultValue(TabAlignment.Top), Description("选中字体颜色")]
        public Color Tab_ForeColor_Selected { get; set; }

        /// <summary>
        /// 标签高度
        /// </summary>
        [Category("自定义-布局"), DefaultValue(typeof(int)), Description("标签高度")]
        public int Tab_Width { get; set; }

        /// <summary>
        /// 标签宽度
        /// </summary>
        [Category("自定义-布局"), DefaultValue(typeof(int)), Description("标签宽度")]
        public int Tab_Height { get; set; }

        /// <summary>
        /// 默认选中索引
        /// </summary>
        [Category("自定义-布局"), DefaultValue(typeof(int)), Description("默认选中索引")]
        public int Tab_Index { get; set; }

        /// <summary>
        /// 导航位置:上下左右
        /// </summary>
        [Category("自定义-布局"), DefaultValue(TabAlignment.Top), Description("导航位置:上下左右")]
        public TabAlignment Alignment { get; set; }

        /// <summary>
        /// 左侧外边距
        /// </summary>
        [Category("自定义-布局"), DefaultValue(typeof(int), "0"), Description("左侧外边距")]
        public int Tab_Margin_Left { get; set; }

        #endregion

        #region 私有属性
        private class Model
        {
            public int Index { get; set; }

            public Button Button { get; set; }

            public Panel Panel { get; set; }
        }

        List<Model> Models = new List<Model>();

        private Panel panel_nav = new Panel();
        private Panel panel_main = new Panel();
        private bool IsFirst = true;
        #endregion

        #region 标签初始化

        /// <summary>
        /// 标签初始化
        /// </summary>
        public void Tab_Load()
        {
            if (this.TabControl != null)
            {
                this.ParentPenel.Controls.Add(this.panel_nav);
                this.ParentPenel.Controls.Add(this.panel_main);

                SetSize();
                GetTabsModels();
                SetTabs();
            }
        }
        #endregion

        #region 设置窗体大小和布局

        /// <summary>
        /// 设置窗体大小和布局
        /// </summary>
        private void SetSize()
        {
            switch (this.Alignment)
            {
                case TabAlignment.Left:
                    {
                        this.MainForm.Width += this.Tab_Width;
                        this.panel_nav.Width = this.Tab_Width;
                        this.panel_nav.Dock = DockStyle.Left;

                        this.panel_main.Location = new Point(this.Tab_Width, 0);
                        this.panel_main.Width = this.MainForm.Width - this.Tab_Width;
                        this.panel_main.Height = this.MainForm.Height;
                        break;
                    }
                case TabAlignment.Right:
                    {
                        this.MainForm.Width += this.Tab_Width;
                        this.panel_nav.Width = this.Tab_Width;
                        this.panel_nav.Dock = DockStyle.Right;

                        this.panel_main.Location = new Point(0, 0);
                        this.panel_main.Width = this.MainForm.Width - this.Tab_Width;
                        this.panel_main.Height = this.MainForm.Height;
                        break;
                    }
                case TabAlignment.Top:
                    {
                        this.MainForm.Height += this.Tab_Height;
                        this.panel_nav.Height = this.Tab_Height;
                        this.panel_nav.Width = this.MainForm.Width;
                        this.panel_nav.Location = new Point(0, 1);
                        //this.panel_nav.Dock = DockStyle.Top;

                        this.panel_main.Location = new Point(0, this.Tab_Height + 2);
                        this.panel_main.Width = this.MainForm.Width;
                        this.panel_main.Height = this.MainForm.Height - this.Tab_Height;
                        break;
                    }
                case TabAlignment.Bottom:
                    {
                        this.MainForm.Height += this.Tab_Height;
                        this.panel_nav.Height = this.Tab_Height;
                        this.panel_nav.Dock = DockStyle.Bottom;

                        this.panel_main.Location = new Point(0, 0);
                        this.panel_main.Width = this.MainForm.Width;
                        this.panel_main.Height = this.MainForm.Height - this.Tab_Height;
                        break;
                    }
            }
            this.TabControl.Visible = false;
            this.panel_nav.BackColor = this.Tab_BackColor;
            //this.panel_main.Dock = DockStyle.Fill;
        }

        #endregion

        #region 获取标签数据

        /// <summary>
        /// 获取标签数据
        /// </summary>
        public void GetTabsModels()
        {
            if (!IsFirst)
            {
                return;
            }
            this.IsFirst = false;
            for (int i = 0; i < this.TabControl.TabPages.Count; i++)
            {
                var item = this.TabControl.TabPages[i];
                var button = new Button()
                {
                    Name = item.Text,
                    Text = item.Text,
                    Width = this.Tab_Width,
                    Height = this.Tab_Height,
                    BackgroundImage = this.Tab_bg_default,
                    FlatStyle = FlatStyle.Flat
                };
                button.FlatAppearance.BorderSize = 0;
                button.Font = item.Font;
                button.FlatAppearance.MouseDownBackColor = Color.White;
                button.FlatAppearance.MouseOverBackColor = Color.White;
                button.Click += Button_Click;
                button.MouseEnter += Button_MouseEnter;
                button.MouseLeave += Button_MouseLeave;

                switch (this.Alignment)
                {
                    case TabAlignment.Left:
                    case TabAlignment.Right:
                        {
                            button.Location = new Point(0, i * this.Tab_Height);
                            break;
                        }
                    case TabAlignment.Top:
                    case TabAlignment.Bottom:
                        {
                            button.Location = new Point(i * this.Tab_Width + this.Tab_Margin_Left, 0);
                            break;
                        }
                }
                Models.Add(new Model() { Index = i, Button = button, Panel = (Panel)item.Controls[0] });
            }
        }
        #endregion

        #region 设置并填充布局按钮

        /// <summary>
        /// 设置并填充布局按钮
        /// </summary>
        private void SetTabs()
        {
            foreach (var item in this.Models)
            {
                this.panel_nav.Controls.Add(item.Button);
                if (item.Index == this.Tab_Index)
                {
                    item.Button.BackgroundImage = this.Tab_bg_selected;
                    item.Button.ForeColor = this.Tab_ForeColor_Selected;
                    this.panel_main.Controls.Clear();
                    this.panel_main.Controls.Add(item.Panel);
                }
            }
        }
        #endregion

        #region 按钮事件

        private void Button_MouseEnter(object sender, EventArgs e)
        {
            ((Button)sender).BackgroundImage = this.Tab_bg_after;
            ((Button)sender).ForeColor = this.Tab_ForeColor_Selected;
        }

        private void Button_MouseLeave(object sender, EventArgs e)
        {
            var model = this.Models.Where(ex => ex.Button.Name == ((Button)sender).Name).FirstOrDefault();
            if (model.Index == this.Tab_Index)
            {
                ((Button)sender).BackgroundImage = this.Tab_bg_selected;
                ((Button)sender).ForeColor = this.Tab_ForeColor_Selected;
            }
            else
            {
                ((Button)sender).BackgroundImage = this.Tab_bg_default;
                ((Button)sender).ForeColor = this.Tab_ForeColor;
            }
        }

        private void Button_Click(object sender, EventArgs e)
        {
            Selected(((Button)sender), this.Tab_Index);
        }
        /// <summary>
        /// 选择标签事件
        /// </summary>
        /// <param name="btn">按钮</param>
        /// <param name="oldIndex">上一个选中索引</param>
        private void Selected(Button btn, int oldIndex)
        {
            var name = btn.Name;
            var obj = this.Models.Where(ex => ex.Button.Name == name).FirstOrDefault().Panel;
            this.panel_main.Controls.Clear();
            this.panel_main.Controls.Add(obj);

            var model = this.Models.Where(ex => ex.Index == oldIndex).FirstOrDefault();
            model.Button.BackgroundImage = this.Tab_bg_default;
            model.Button.ForeColor = this.Tab_ForeColor;

            var model1 = this.Models.Where(ex => ex.Button.Name == name).FirstOrDefault();
            this.Tab_Index = model1.Index;

            btn.BackgroundImage = this.Tab_bg_selected;
            btn.ForeColor = this.Tab_ForeColor_Selected;
        }
        #endregion

        #region 设置选中标签

        /// <summary>
        /// 设置选中标签
        /// </summary>
        /// <param name="index"></param>
        public void SelectTab(int index)
        {
            var oldIndex = this.Tab_Index;
            this.Tab_Index = index;
            SetTabs();

            Selected(this.Models[index].Button, oldIndex);
        }
        #endregion
    }
}
